package com.doublethread.easymart.modules.user.controller;

import com.doublethread.easymart.common.context.UserContext;
import com.doublethread.easymart.common.result.Result;
import com.doublethread.easymart.modules.user.dto.UserPasswordUpdateDTO;
import com.doublethread.easymart.modules.user.dto.UserUpdateDTO;
import com.doublethread.easymart.modules.user.service.UserService;
import com.doublethread.easymart.modules.user.vo.UserVO;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户端用户控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 获取个人信息
     */
    @GetMapping("/profile")
    public Result<UserVO> getProfile() {
        Long userId = UserContext.getCurrentUserId();

        log.info("获取用户个人信息");

        UserVO userVO = userService.getUserProfile(userId);

        log.info("获取用户个人信息成功");
        return Result.success(userVO);
    }

    /**
     * 更新个人信息
     */
    @PutMapping("/profile")
    public Result<Void> updateProfile(@Valid @RequestBody UserUpdateDTO updateDTO) {
        Long userId = UserContext.getCurrentUserId();

        log.info("更新用户信息");

        userService.updateUser(userId, updateDTO);

        log.info("更新用户信息成功");
        return Result.success();
    }

    /**
     * 修改密码
     */
    @PutMapping("/password")
    public Result<Void> updatePassword(@Valid @RequestBody UserPasswordUpdateDTO passwordUpdateDTO) {
        Long userId = UserContext.getCurrentUserId();

        log.info("修改密码");

        userService.updatePassword(userId, passwordUpdateDTO);

        log.info("修改密码成功");
        return Result.success();
    }
}
